package com.gyx.rocsun.common.config;


import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;

import java.time.Duration;

@Configurable
public class CaffeineConfig {

    @Bean
    public Caffeine<Object, Object> caffeine() {
        return Caffeine.newBuilder()
                .expireAfterWrite(Duration.ofMinutes(5));
    }

    @Bean
    public CacheManager cacheManager(Caffeine<Object, Object> cache) {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager("CacheService");
        cacheManager.setCaffeine(cache);

        return cacheManager;
    }

}
